<template>
  <div class="app-container">
    <div class="manage-header">
      <el-form ref="Form" :inline="true" :model="Form" :rules="Rules">
        <el-form-item prop="turbineId">
          <el-input v-model="Form.turbineId" placeholder="请输入机组编号" :validate-event="false" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
        <el-button v-if="isShow1" type="primary" @click="getData">取消查询</el-button>
        <el-button type="primary" @click="handleImport">导入</el-button>
        <el-button type="primary" @click="handleExport">导出</el-button>
      </el-form>
    </div>
    <el-dialog
      title="导入Excel"
      :visible.sync="ImportDialog"
      width="30%"
      :before-close="handleClose"
    >
      <div
        style="margin-top: 30%"
      >
        <el-upload
          ref="upload"
          class="upload-demo"
          :action="actionAddress"
          :auto-upload="false"
          :on-success="handlemessage"
          style="position: absolute;left: 50%;top: 50%;transform: translate(-50%,-50%);"
        >
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器
          </el-button>
          <div slot="tip" class="el-upload__tip">请上传想要导入的Excel文件</div>
        </el-upload>
      </div>
    </el-dialog>
    <el-dialog
      title="导出Excel"
      :visible.sync="ExportDialog"
      width="30%"
      :before-close="handleClose"
    >
      <div style="float: top">
        <div
          style="position:relative;top:10%;bottom:0;left: 0;
            margin: auto;right: 0;
            text-align:center"
        >
          <el-button type="primary" @click="handleExport1">导出所有数据</el-button>
          <el-button type="primary" @click="handleExport2">导出模板</el-button>
        </div>
        <div width="100%">
          <h3 style="text-align: center">你可以选择“导出模板”导出一张空白Excel</h3>
        </div>
      </div>
    </el-dialog>
    <el-table
      stripe
      border
      height="485"
      width="500"
      :data="tableData"
      style="width: 100%"
      :header-cell-style="{'text-align': 'center'}"
    >
      <el-table-column
        prop="faultInformation"
        label="记录信息"
        width="600"
        align="center"
      >
        <el-table-column
          prop="faultTurbine"
          label="故障机组编号"
          width="125"
          align="center"
        />
        <el-table-column
          prop="faultName"
          label="故障名称"
          width="100"
          align="center"
        />
        <el-table-column
          prop="faultId"
          label="故障代码"
          width="100"
          align="center"
        />
        <el-table-column
          prop="faultDescribe"
          label="故障描述"
          width="200"
          align="center"
        />
        <el-table-column
          prop="faultType"
          label="故障类别"
          width="100"
          align="center"
        />
      </el-table-column>
      <el-table-column
        prop="timeInformation"
        label="时间信息"
        width="300"
        align="center"
      >
        <el-table-column
          prop="faultReportingTime"
          label="故障报出时间"
          width="100"
          align="center"
        />
        <el-table-column
          prop="maintenanceTime"
          label="故障处理时间"
          width="125"
          align="center"
        >
          <template slot="header">
            <div>开始处理时间</div>
            <div>维护处理时间</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="resetRunningTime"
          label="复位运行时间"
          width="100"
          align="center"
        />
      </el-table-column>
      <el-table-column
        prop="faultLocate"
        label="故障定位"
        width="300"
        align="center"
      >
        <el-table-column
          prop="faultLocationOne"
          label="故障位置一级"
          align="center"
        />
        <el-table-column
          prop="faultLocationTwo"
          label="故障位置二级"
          align="center"
        />
        <el-table-column
          prop="faultLocationThree"
          label="故障位置三级"
          align="center"
        />
      </el-table-column>
      <el-table-column
        prop="faultCheck"
        label="故障诊断"
        width="300"
        align="center"
      >
        <el-table-column
          prop="checkItems"
          label="排查项目"
          width="100"
          align="center"
        />
        <el-table-column
          prop="faultCause"
          label="故障原因"
          align="center"
          width="300"
        />
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page.sync="currentPage"
      :page-size="pageSize"
      layout="total, prev, pager, next, jumper"
      :total="total"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { getAllFaultInfo, getFaultInfo } from '@/api/fault_analysis'
import axios from 'axios'

export default {
  name: 'FaultTable',
  data() {
    // const validateNum = (rule, value, callback) => {
    //   if (!(/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/).test(value)) {
    //     callback(new Error('请输入数字值'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      total: 1,
      pageSize: 1,
      currentPage: 1,
      tableData: [],
      isShow1: false,
      ImportDialog: false,
      ExportDialog: false,
      actionAddress: process.env.VUE_APP_BASE_API + '/fault/import',
      Form: {
        turbineId: ''
      },
      Rules: {
        turbineId: [
          { required: true, trigger: 'blur', message: '请输入机组编号' }
        ]
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    getData() {
      getAllFaultInfo().then(({ data }) => {
        this.total = data.page.total
        this.pageSize = data.page.size
        this.tableData = data.page.records
        this.isShow1 = false
        this.Form.turbineId = ''
      })
    },
    onSubmit() {
      this.$refs.Form.validate((valid) => {
        if (valid) {
          getFaultInfo(this.Form.turbineId).then(({ data }) => {
            if (data.page.total === 0) {
              this.$message({
                showClose: true,
                message: '没有查询到相应的风电机组故障信息',
                type: 'error'
              })
            } else {
              this.total = data.page.total
              this.pageSize = data.page.size
              this.tableData = data.page.records
              this.isShow1 = true
            }
          })
        }
      })
    },
    handleCurrentChange(pageNum) {
      getAllFaultInfo(pageNum).then(({ data }) => {
        this.total = data.page.total
        this.pageSize = data.page.size
        this.tableData = data.page.records
      })
    },
    handleClose() {
      // 关闭弹窗
      this.ImportDialog = false
      this.ExportDialog = false
    },
    handleImport() {
      this.ImportDialog = true
    },
    handleExport() {
      this.ExportDialog = true
    },
    submitUpload() {
      this.$refs.upload.submit()
    },
    handlemessage() {
      this.$message({
        type: 'success',
        message: '已成功上传文件'
      })
      this.getData();
    },
    handleExport1() {
      axios({ // 用axios发送get请求
        method: 'get',
        url: process.env.VUE_APP_BASE_API + '/fault/export', // 请求地址
        responseType: 'blob', // 表明返回服务器返回的数据类型
        headers: {
          'Content-Type': 'application/json',
          'getResponse': 'true'
        }
      }).then(res => { // 处理返回的文件流
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })

        const disposition = res.headers['content-disposition']
        const filename = disposition.split(/filename=/i).pop()
        const fileName = decodeURI(filename)

        const elink = document.createElement('a')
        elink.download = fileName
        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        URL.revokeObjectURL(elink.href) // 释放URL对象
        document.body.removeChild(elink)
      })
    },
    handleExport2() {
      axios({ // 用axios发送get请求
        method: 'get',
        url: process.env.VUE_APP_BASE_API + '/fault/extemplate', // 请求地址
        responseType: 'blob', // 表明返回服务器返回的数据类型
        headers: {
          'Content-Type': 'application/json',
          'getResponse': 'true'
        }
      }).then(res => { // 处理返回的文件流
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })

        const disposition = res.headers['content-disposition']
        const filename = disposition.split(/filename=/i).pop()
        const fileName = decodeURI(filename)

        const elink = document.createElement('a')
        elink.download = fileName
        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        URL.revokeObjectURL(elink.href) // 释放URL对象
        document.body.removeChild(elink)
      })
    }
  }

}
</script>

<style>

</style>
